Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
[Javascript]: combina dos matrices de objetos que tienen valores de cadena y matriz
const FirstData = [ { title: 'Title 1', data: ['foo1', 'foo2', 'foo3', 'foo4', 'foo5'], }, { title: 'Title 2', data: ['a', 'b', 'c'], }, { title: 'Title 3', data: ['hello1', 'hello2', 'hello3', 'hello4', 'hello5'], }, ];

Los datos de SecondData se barajan tras bambalinas

 const SecondData = [ { title: 'Title 1', data: ['foo3', 'foo1', 'foo2'], }, { title: 'Title 2', data: ['b', 'c', 'a'], }, { title: 'Title 3', data: ['hello1', 'hello3', 'hello2'], }, ];

SALIDA ESPERADA (debe ser similar a SecondData pero las nuevas cadenas de los datos de FirstData deben insertarse, agregarse o fusionarse):

 [ { title: 'Title 1', data: ['foo3', 'foo1', 'foo2', 'foo4', 'foo5'], }, { title: 'Title 2', data: ['b', 'c', 'a'], }, { title: 'Title 3', data: ['hello1', 'hello3', 'hello2', 'hello4', 'hello5'], }, ];
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede crear un mapa que indexe según el título y luego usar Set para asegurarse de no agregar duplicados. Algo como esto:

 const items = {} firstData.forEach(item => { items[item.title] = new Set([...item.data)] }) secondData.forEach(item => { const existingItem = items[item.title] if(!existingItem) { items[item.title] = new Set([...item.data)] } else { items[item.title] = new Set([...existingItem, ...item.data]) } }) const merged = Object.keys(items).map(title => ({ title: key, data: [...items[key]] }) )
about 4 years ago · Juan Pablo Isaza Report

0

Sugeriría concatenar las matrices y luego usar Array.reduce() para agrupar los valores por title .

Para cada entrada, fusionamos la matriz de datos usando un Set , que descartará cualquier valor duplicado en esta matriz.

 const FirstData = [ { title: 'Title 1', data: ['foo1', 'foo2', 'foo3', 'foo4', 'foo5'], }, { title: 'Title 2', data: ['a', 'b', 'c'], }, { title: 'Title 3', data: ['hello1', 'hello2', 'hello3', 'hello4', 'hello5'], }, ]; const SecondData = [ { title: 'Title 1', data: ['foo3', 'foo1', 'foo2'], }, { title: 'Title 2', data: ['b', 'c', 'a'], }, { title: 'Title 3', data: ['hello1', 'hello3', 'hello2'], }, ]; // Concatenate our data to get input... const input = FirstData.concat(SecondData); // Group each item by title. // For each item, merge the data array using a Set object const result = Object.values(input.reduce((acc, { title, data }) => { acc[title] = acc[title] || { title, data: [] }; acc[title].data = [...new Set([...acc[title].data, ...data])]; return acc; }, {})); console.log('Result:', result)

about 4 years ago · Juan Pablo Isaza Report

0

Puede tomar un Set y filtrar la matriz dada a partir de valores aleatorios.

 const merge = (a, b) => [ ...a, ...b.filter((s => v => !s.has(v))(new Set(a))) ], given = ['foo1', 'foo2', 'foo3', 'foo4', 'foo5'], random = ['foo3', 'foo1', 'foo2'], result = merge(random, given); console.log(...result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!